const {xhsRemote} = require('../utils/xhs/xhsRemote')
const {xhsCookies} = require('../utils/xhs/cookies')
const { getMainWindow } = require('ee-core/electron');
const axios = require('axios');
const qs = require('qs');

class xhsController {

    /**
     * 获取作品信息
     * @param config
     * @returns {Promise<*>}
     */
    async getWorkInfo(config) {
        const {workId, xsecToken} = config
        const params = {"extra": {"need_body_topic": "1"}, "source_note_id": workId, "xsec_source": "pc_feed", "image_formats": ["jpg", "webp", "avif"], "xsec_token": xsecToken}
        // const headers = {"cookie": webviewCookieToHeaderCookie(JSON.parse(acc.cookies))}
        const headers = {"cookie": xhsCookies}
        const response = await xhsRemote('/api/sns/web/v1/feed', params, headers, true, true)
        // console.log("【小红书】指定作品详细信息：", response)
        return response
    }

    /**
     * 短链还原
     * @param config
     * @returns {Promise<Promise<AxiosResponse<any>> | *>}
     */
    async linkRestoration(config) {
        const {url} = config
        console.log('短链还原：', url)
        let timestamp = new Date().getTime()
        const res = await axios({
            method: 'post',
            url: `https://duanwangzhihuanyuan.bmcx.com/web_system/bmcx_com_www/system/file/duanwangzhihuanyuan/get/?ajaxtimestamp=${timestamp}`,
            headers: {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0'
            },
            data: qs.stringify({
                'turl': url
            })
        });
        return JSON.stringify(res.data)
    }
}

xhsController.toString = () => '[class xhsController]';

module.exports = xhsController;